package com.oasystem.web.controller.fund;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oasystem.service.fund.AppropriationApplicationService;
import com.oasystem.service.fund.FundApplicationService;

@Controller
@RequestMapping(value = "/MainMenu/fund")
public class FundSummaryController {
	@Resource(name = "fundApplicationService")
	private FundApplicationService fundApplicationService;
	@Resource(name = "appropriationApplicationService")
	private AppropriationApplicationService appropriationApplicationService;

	@RequestMapping(value = "/fundSummaryPage")
	public String fundSummaryPage(Model model) {
		model.addAttribute("mainMenu", "fund");
		model.addAttribute("subMenu", "fundStatistica");
		// 经费总支出
		BigDecimal totalFundOutcome = new BigDecimal(
				fundApplicationService.totalFundOutcome());
		// 精确到两位，四舍五入
		model.addAttribute("totalFundOutcome",
				totalFundOutcome.setScale(2, BigDecimal.ROUND_HALF_DOWN)
						.doubleValue());
		// 拨款总支出
		BigDecimal totalAppropriationOutcome = new BigDecimal(
				appropriationApplicationService.totalAppropriationOutcome());
		model.addAttribute(
				"totalAppropriationOutcome",
				totalAppropriationOutcome.setScale(2,
						BigDecimal.ROUND_HALF_DOWN).doubleValue());
		// 经费分类统计
		model.addAttribute("fundOutcomeSummaryList",
				fundApplicationService.fundOutcomeSummaryList());

		return "fund.summary";
	}

	@RequestMapping(value = "/rejectFundApplication")
	public @ResponseBody
	ModelMap rejectFundApplication(Model model, String fundApplicationId,
			String reason, HttpSession session) {
		ModelMap map = new ModelMap();
		try {
			fundApplicationService.rejectFundApplication(fundApplicationId,
					reason);
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/searchSummary")
	public String searchSummary(Model model, String from, String to,
			HttpSession session) {
		ModelMap map = new ModelMap();
		try {
			model.addAttribute("fundOutcomeSummaryList",
					fundApplicationService.searchSummary(from, to));
		} catch (Exception e) {
			map.addAttribute("error", e.getMessage());
		}
		return "fund.summarySearchResult";
	}
}
